package com.nebula.service.impl;

import com.nebula.entity.Role;
import com.nebula.entity.User;
import com.nebula.common.PageResult;
import com.nebula.common.Result;
import com.nebula.dao.IRoleDao;
import com.nebula.dao.IUserDao;
import com.nebula.dao.impl.RoleDaoImpl;
import com.nebula.dao.impl.UserDaoImpl;
import com.nebula.service.IUserService;
import com.nebula.util.DateTimeUtil;
import com.nebula.util.MD5Util;
import com.nebula.vo.UserVo;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CcccX
 * @create 2019-01-09 14:53
 */
public class UserServiceImpl implements IUserService {
    private IUserDao iUserDao = new UserDaoImpl();
    private IRoleDao iRoleDao = new RoleDaoImpl();
    @Override
    public Result login(String username, String password, HttpServletRequest request) {
        int resultCount = iUserDao.checkLoginUsername(username);
        if (resultCount == 0) {
            return Result.createByErrorMessage("用户名不存在");
        }
        password = MD5Util.MD5EncodeUtf8(password);
        resultCount = iUserDao.login(username, password);
        if (resultCount == 0) {
            return Result.createByErrorMessage("账号或密码错误");
        }

        User user = iUserDao.findByUsername(username);
        Role role = iRoleDao.findById(user.getRoleId());
        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setGender(user.getGender());
        userVo.setCreatedTime(DateTimeUtil.dateToStr(user.getCreatedTime()));
        userVo.setRoleId(user.getRoleId());
        userVo.setRealname(user.getRealname());
        userVo.setRoleName(role.getRole());
        userVo.setUsername(user.getUsername());
        userVo.setUpdatedTime(DateTimeUtil.dateToStr(user.getUpdatedTime()));
        return Result.createBySuccess("登录成功", userVo);
    }

    @Override
    public int save(User user) {
        return iUserDao.save(user);
    }

    @Override
    public boolean checkNavRole(Integer roleId, String navId) {
        return iUserDao.checkNavRole(roleId, navId);
    }

    @Override
    public void update(User user) {
        iUserDao.update(user);
    }

    @Override
    public User findById(String id) {
        return iUserDao.findById(id);
    }

    @Override
    public List<User> findAll() {
        return iUserDao.findAll();
    }

    @Override
    public PageResult<UserVo> findAllAndPage(int page, int size) {
        List<User> users = iUserDao.findByPage(page, size);
        List<Role> roles = iRoleDao.findAll();
        List<UserVo> userVos = new ArrayList<>();
        for (User user : users) {
            UserVo userVo = assembleSalaryVo(user);
            for (Role role : roles) {
                if (role.getId() == user.getRoleId()){
                    userVo.setRoleName(role.getRole());
                    break;
                }
            }
            userVos.add(userVo);
        }
        PageResult<UserVo> pageResult = new PageResult<>();
        pageResult.setCurrPage(page);
        pageResult.setList(userVos);

        Integer totalCount = iUserDao.selectCount("user");
        Double dc = Math.ceil(totalCount.doubleValue() / size);
        pageResult.setTotalCount(totalCount);
        pageResult.setPageSize(dc.intValue());

        return pageResult;
    }

    @Override
    public int delete(String id){
        return iUserDao.delete(id);
    }

    @Override
    public User findByUsername(String username) {
        return iUserDao.findByUsername(username);
    }

    private UserVo assembleSalaryVo(User user) {
        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setUsername(user.getUsername());
        userVo.setRealname(user.getRealname());
        userVo.setGender(user.getGender());
        userVo.setRoleId(user.getRoleId());
        userVo.setCreatedTime(DateTimeUtil.dateToStr(user.getCreatedTime()));
        userVo.setUpdatedTime(DateTimeUtil.dateToStr(user.getUpdatedTime()));
        return userVo;
    }
}
